package com.example.demo.demos.repository;

import com.example.demo.demos.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {
    User findByNameAndPassword(String name, String password);

    // 使用@Query注解编写复杂SQL
    @org.springframework.data.jpa.repository.Query(value = "SELECT u FROM User u WHERE u.name = ?1 AND u.age > ?2")
    User findByNameAndAgeGreaterThan(String name, int age);

    // 原生SQL示例
    @org.springframework.data.jpa.repository.Query(value = "SELECT * FROM user WHERE name = ?1 AND age > ?2", nativeQuery = true)
    User findByNameAndAgeNative(String name, int age);

    // 多表关联并获取多个表字段（JPQL示例）
    @org.springframework.data.jpa.repository.Query("SELECT u.name, u.age, r.name FROM User u JOIN u.roles r WHERE r.name = ?1")
    java.util.List<Object[]> findUserNameAgeAndRoleNameByRole(String roleName);

    // 多表关联并获取多个表字段（原生SQL示例）
    @org.springframework.data.jpa.repository.Query(value = "SELECT u.name, u.age, r.name as role_name FROM user u JOIN user_role ur ON u.id = ur.user_id JOIN role r ON ur.role_id = r.id WHERE r.name = ?1", nativeQuery = true)
    java.util.List<Object[]> findUserNameAgeAndRoleNameByRoleNative(String roleName);


}
